<?php


namespace App\Http\Controllers\Admin\RoomManager;

//用户操作纪录
use App\Response\Response;
use Illuminate\Http\Request;
use App\Exceptions\BasicException;
use App\Http\Controllers\BaseController;

//客户端房间操作纪录
class RoomUserOperationLogsController extends BaseController
{
    /**
     * @api {get} /room/client-log 客户端操作纪录
     * @apiVersion 0.1.0
     * @apiName 客户端操作纪录
     * @apiGroup 房间管理
     * @apiParam {Int}                     page 产出限制 （选填）
     * @apiParam {Int}                     page_size 历史产出限制 （选填）
     * @apiParam {Int}                     op_type 动作  1:加入房间 2：正常退出房间 3：掉线退出 4：踢出房间 5上麦 6下麦 7抱上麦 8抱下麦 9锁麦 10锁全部麦位 11解锁麦位 12解锁全部麦位 13禁言 14取消禁言 15设置麦位倒计时 16清除麦位心动 17发送互动表情 18修改房间信息 19赠送礼物
     * @apiParam {Int}                     user_id 操作用户id（选填）
     * @apiParam {Int}                     to_user_id 指定用户id（选填）
     * @apiParam {Int}                     op_time 操作时间 秒级时间戳（选填）
     * @apiSuccessExample 成功响应
     * {
     * "code": 200,
     * "data": {
     * "total": 531,
     * "current_page": 1,
     * "last_page": 266,
     * "list": [
     * {
     * "id": 534,
     * "source": 1,
     * "room_id": 3287,//房间id
     *
     * "op_user_id": 1317243,
     * "user_id": 643460,
     * "op_type": 19, //动作 1:加入房间 2：正常退出房间 3：掉线退出 4：踢出房间 5上麦 6下麦 7抱上麦 8抱下麦 9锁麦 10锁全部麦位 11解锁麦位 12解锁全部麦位 13禁言 14取消禁言 15设置麦位倒计时 16清除麦位心动 17发送互动表情 18修改房间信息 19赠送礼物
     * "content": "赠送金币礼物告白玫瑰X1",//操作内容
     * "remark": "",
     * "params": "[]",
     * "create_time": 1629971306,//创建时间
     * "update_time": 1629971306,
     * "op_user": {
     * "user_id": 1317243,//操作用户id
     * "nickname": "\"拓拔妙3390724\""//操作用户昵称
     * },
     * "user": {
     * "user_id": 643460,//操作对象用户id
     * "nickname": "用户209474"//操作对象用户昵称
     * },
     * "room": {
     * "id": 3287,//房间id
     * "room_name": "维秘"//房间名
     * }
     * },
     * {
     * "id": 533,
     * "source": 1,
     * "room_id": 3287,
     * "op_user_id": 1317243,
     * "user_id": 643628,
     * "op_type": 19,
     * "content": "赠送金币礼物告白玫瑰X1",
     * "remark": "",
     * "params": "[]",
     * "create_time": 1629971306,
     * "update_time": 1629971306,
     * "op_user": {
     * "user_id": 1317243,
     * "nickname": "\"拓拔妙3390724\""
     * },
     * "user": {
     * "user_id": 643628,
     * "nickname": "用户7248479"
     * },
     * "room": {
     * "id": 3287,
     * "room_name": "维秘"
     * }
     * }
     * ]
     * }
     * }
     * @apiErrorExample 错误响应
     *      {
     *          "code": 10001,
     *          "message": "errorMsg"
     *      }
     */
    public function logList(Request $request)
    {
        $param = $request->only(['op_type', 'op_time', 'user_id', 'to_user_id', 'page', 'page_size']);
        $v = app('validator')->make($param,
            [
                'op_type'    => 'filled|in:1,2,3,4,5,6,7,8,9',
                'op_time'    => 'filled|integer|min:1',
                'user_id'    => 'filled|integer|min:1',
                'to_user_id' => 'filled|integer|min:1',
                'page'       => 'filled|integer|min:1',
                'page_size'  => 'filled|integer|min:1',
            ]);
        if ($v->fails()) {
            throw new BasicException(10001, $v->errors()->first());
        }
        $param['page'] = $param['page'] ?? 1;
        $param['page_size'] = $param['page_size'] ?? 5;
        $where = [];

        if (isset($param['user_id'])) {
            $where[] = ['op_user_id', '=', $param['user_id']];
        }
        if (isset($param['to_user_id'])) {
            $where[] = ['user_id', '=', $param['to_user_id']];
        }
        if (isset($param['op_type'])) {
            $where[] = ['op_type', '=', $param['op_type']];
        }
        if (isset($param['op_time'])) {
            $where[] = ['create_time', '>=', $param['op_time']];
        }
        $funUser = function ($query) {
            $query->select(['user_id', 'nickname']);
        };
        $funRoom = function ($query) {
            $query->select(['id', 'room_name']);
        };
        $model = app('model_room_op_log')
            ->orderBy('id', 'desc')
            ->with(['opUser' => $funUser, 'user' => $funUser, 'room' => $funRoom]);
        if (empty($where)) {
            $res = $model->paginate($param['page_size'], ['*'], '', $param['page'])->toArray();
        } else {
            $res = $model->where($where)->paginate($param['page_size'], ['*'], '', $param['page'])->toArray();
        }
        //返回值有固定格式
        $result = [
            'total'        => $res['total'] ?? 0,
            'current_page' => $res['current_page'] ?? 1,
            'last_page'    => $res['last_page'] ?? 1,
            'list'         => $res['data'],
        ];

        return Response::sendData($result);
    }

}